package com.lxg.springboot.controller;

import java.util.List;

import com.lxg.springboot.entity.User;
import com.lxg.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class HelloWorldRestController {

    @Autowired
    UserService userService;  //Service which will do all data retrieval/manipulation work


    //-------------------Retrieve All Users--------------------------------------------------------

    @PreAuthorize("hasRole('USER')")
    @RequestMapping(value = "/user/", method = RequestMethod.GET)
    @ResponseBody
    public List<User> listAllUsers() {
        List<User> users = userService.findAll();
        if (users.isEmpty()) {
            return null;
        }
        return users;
    }

    @RequestMapping(value = "/hello/", method = RequestMethod.GET)
    @ResponseBody
    public String hello() {
        return "hello";
    }

}